package cloudoer.blog.service.impl;

import cloudoer.blog.base.BaseService;
import cloudoer.blog.entity.User;
import cloudoer.blog.exception.ServiceException;
import cloudoer.blog.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl extends BaseService implements UserService {

    private static Logger logger = Logger.getLogger(UserServiceImpl.class);

    public User getById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public List<User> getAll() {
        return userMapper.selectCondition(new HashMap());
    }

    public void add(User user) {
        Map map = new HashMap();
        List<User> list = null;
        //检查邮箱
        if (user.getEmail() == null){
            throw new ServiceException("邮箱验证失败");
        }else {
            map.clear();
            map.put("u.email", user.getEmail());
            map.put("u.state", "0");
            list = userMapper.selectCondition(map);
            if (list != null && list.size() > 0){
                throw new ServiceException("该邮箱已注册本站");
            }
        }
        //检查用户名
        if (StringUtils.isNotBlank(user.getUsername().trim())){
            map.clear();
            map.put("u.userName", user.getUsername());
            list = userMapper.selectCondition(map);
            if (list != null && list.size() > 0){
                throw new ServiceException("用户名已被占用，重新换一个吧");
            }
        }else {
            user.setUsername(user.getEmail());
        }
        user.setState("-1");
        userMapper.deleteByEmail(user.getEmail());
        userMapper.insertSelective(user);
    }

    public void update(User user) {
        Map map = new HashMap();
        List<User> list = null;
        //检查用户名
        if (StringUtils.isNotBlank(user.getUsername())){
            map.clear();
            map.put("u.userName", user.getUsername());
            list = userMapper.selectCondition(map);
            if (list != null && list.size() > 0){
                throw new ServiceException("用户名已被占用，重新换一个吧");
            }
        }
        userMapper.updateByPrimaryKeySelective(user);
    }

    public void delete(String id) {
        userMapper.deleteByPrimaryKey(id);
    }

    public User login(User user) {
        Map map = new HashMap();
        map.put("u.userName",user.getUsername());
        map.put("u.password", user.getPassword());
        List<User> users = userMapper.selectCondition(map);
        if (users != null && users.size() > 0){
            return users.get(0);
        }
        map.clear();
        map.put("u.email",user.getEmail());
        map.put("u.password", user.getPassword());
        users = userMapper.selectCondition(map);
        if (users != null && users.size() > 0){
            return users.get(0);
        }
        return null;
    }

    public User validation(User user){
        Map map = new HashMap();
        map.put("u.id",user.getId());
        map.put("u.state", "-1");
        List<User> list = userMapper.selectCondition(map);
        if (list != null || list.size() > 0){
            user.setState("0");
            update(user);
            return userMapper.selectByPrimaryKey(user.getId());
        }else {
            return null;
        }
    }

}
